﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace HoaDon
{
	public partial class LocHoaDonNK : System.Web.UI.Page
	{
		private System.Globalization.CultureInfo cultureinfo = new System.Globalization.CultureInfo("vi-VN");
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				dttuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
				dtdenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");

				drnhanvien.DataSource = Get_NhanVien();
				drnhanvien.DataValueField = "UserID";
				drnhanvien.DataTextField = "UserName";
				drnhanvien.DataBind();
				drnhanvien.SelectedValue = System.Web.HttpContext.Current.User.Identity.Name.ToUpper();

				drcuahang.DataSource = Get_CuaHang();
				drcuahang.DataValueField = "KhoID";
				drcuahang.DataTextField = "KhoName";
				drcuahang.DataBind();
				drcuahang.SelectedValue = "All";

				gdxdshoadon.DataSource = Get_Data();
				gdxdshoadon.DataBind();
				if (gdxdshoadon.Rows.Count > 0)
					gdxdshoadon.Rows[gdxdshoadon.Rows.Count - 1].Cells[8].Enabled = false;
			}
		}

		protected void btOk_Click(object sender, EventArgs objec)
		{
			gdxdshoadon.DataSource = Get_Data();
			gdxdshoadon.DataBind();
			if (gdxdshoadon.Rows.Count > 0)
				gdxdshoadon.Rows[gdxdshoadon.Rows.Count - 1].Cells[8].Enabled = false;
		}

		private DataTable Get_Data()
		{
			string sCommand = "";
			sCommand += " AND Ngay_ct >= '" + DateTime.Parse(dttuNgay.Text, cultureinfo).ToString("yyyyMMdd") + "'";
			sCommand += " AND Ngay_ct <= '" + DateTime.Parse(dtdenNgay.Text, cultureinfo).ToString("yyyyMMdd") + "'";

			if (drnhanvien.SelectedValue != null)
			{
				if (drnhanvien.SelectedValue.ToString() != "All")
					sCommand += " AND FK_bpkdID = '" + drnhanvien.SelectedValue.ToString() + "'";
			}

			if (drcuahang.SelectedValue != null)
			{
				if (drcuahang.SelectedValue.ToString() != "All")
					sCommand += " AND FK_khoID = '" + drcuahang.SelectedValue.ToString() + "'";
			}

			if (ckAll.Checked == false)
				sCommand += " AND Status = '1'";

			DataTable dtResult = new DataTable();
			dtResult = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM vph74 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "'" + sCommand + " ORDER BY Ngay_ct");

			if (dtResult.Rows.Count > 0)
			{
				decimal t_sl = 0;
				decimal t_tien = 0;
				for (int i = 0; i < dtResult.Rows.Count; i++)
				{
					t_sl += Convert.ToDecimal(dtResult.Rows[i]["T_Sl"].ToString());
					t_tien += Convert.ToDecimal(dtResult.Rows[i]["T_Tien"].ToString());
				}
				DataRow dr = dtResult.NewRow();
				dr["Stt_rec"] = "TỔNG CỘNG";
				dr["T_Sl"] = "" + t_sl;
				dr["T_Tien"] = "" + t_tien;
				dtResult.Rows.Add(dr);
			}
			return dtResult;
		}

		protected void btIn_Click(object sender, EventArgs objec)
		{
			//DataTable dtReport = new DataTable();
			//dtReport = Get_Data();
			//rptBCBKBH rpt = new rptBCBKBH { DataSource = dtReport };
			//rpt.ma_tu_dong.DataBindings.Add("Text", dtReport, "STT_rec");
			//rpt.ngay_ct.DataBindings.Add("Text", dtReport, "Ngay_ct", "{0:dd/MM/yyyy}");
			//rpt.ban.DataBindings.Add("Text", dtReport, "vvName");
			//rpt.tong_tien.DataBindings.Add("Text", dtReport, "T_tien", "{0:#,#}");
			//rpt.nguoi_tao.DataBindings.Add("Text", dtReport, "user_id0");
			//rpt.ngay_tao.DataBindings.Add("Text", dtReport, "datetime0", "{0:dd/MM/yyyy}");
			//rpt.nguoi_sua.DataBindings.Add("Text", dtReport, "user_id2");
			//rpt.ngay_sua.DataBindings.Add("Text", dtReport, "datetime2", "{0:dd/MM/yyyy}");

			//rpt.Parameters["tungay"].Value = dttuNgay.Text;
			//rpt.Parameters["denngay"].Value = dtdenNgay.Text;
			//rpt.Parameters["khuvuc"].Value = drkhuvuc.SelectedItem.ToString();
			//rpt.Parameters["tenban"].Value = drban.SelectedItem.ToString();
			//Session["Report"] = rpt;
			//Response.Redirect("~\\Print.aspx");
		}

		protected void btXoa_Click(object sender, EventArgs ap)
		{
			ImageButton bt = (ImageButton)sender;
			SqlEXEC.ExecCommand(Session["strConn"].ToString(), "EXEC [fs_DeletePND] '" + bt.CommandArgument + "','" + Session["FK_DatabaseID"].ToString() + "', '1'");

			gdxdshoadon.DataSource = Get_Data();
			gdxdshoadon.DataBind();
			if (gdxdshoadon.Rows.Count > 0)
				gdxdshoadon.Rows[gdxdshoadon.Rows.Count - 1].Cells[8].Enabled = false;
		}

		protected void btXem_Click(object sender, EventArgs ap)
		{
			//ImageButton bt = (ImageButton)sender;
			//string hoadon_tien = bt.CommandArgument;
			//string[] s = hoadon_tien.Split('_');
			//Session["vPND_Stt_Rec"] = s[0];
			//Response.Redirect("~\\DSMon.aspx");
		}

		private DataTable Get_NhanVien()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(WebConfigurationManager.ConnectionStrings["proxyconn"].ConnectionString, "SELECT UserID, UserName FROM Userinfo WHERE FK_DatabaseID = (SELECT FK_DatabaseID FROM Userinfo a WHERE a.UserID = '" + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "')");
			DataRow newR = dt.NewRow();
			newR["UserID"] = "All";
			newR["UserName"] = "Tất cả";
			dt.Rows.Add(newR);
			return dt;
		}

		private DataTable Get_CuaHang()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmkho WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND Status = '1'");
			DataRow newR = dt.NewRow();
			newR["KhoID"] = "All";
			newR["KhoName"] = "Tất cả";
			dt.Rows.Add(newR);
			return dt;
		}
	}
}
